package com.jin.webvideo.externalInterfaces.youku;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jin.webvideo.externalInterfaces.PlatformSupport;
import com.jin.webvideo.externalInterfaces.entity.Filmography;
import com.jin.webvideo.externalInterfaces.entity.FilmographyInfo;
import com.jin.webvideo.externalInterfaces.entity.Participation;
import com.jin.webvideo.externalInterfaces.entity.ParticipationMate;
import com.jin.webvideo.util.base.StringUtil;
import com.jin.webvideo.util.base.URLUtil;
import org.springframework.stereotype.Component;

import javax.script.ScriptException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Component
public class YouKuBaseInfo {
    private static String data = "{\"ms_codes\":\"2019030100\",\"params\":\"{\\\\\"biz\\\\\":true,\\\\\"scene\\\\\":\\\\\"component\\\\\",\\\\\"componentVersion\\\\\":\\\\\"3\\\\\",\\\\\"ip\\\\\":\\\\\"113.128.101.51\\\\\",\\\\\"debug\\\\\":0,\\\\\"utdid\\\\\":\\\\\"1TH+GgpleHkCAXGAM+lnFijw\\\\\",\\\\\"userId\\\\\":\\\\\"\\\\\",\\\\\"platform\\\\\":\\\\\"pc\\\\\",\\\\\"nextSession\\\\\":\\\\\"{\\\\\\\\\\\\\"componentIndex\\\\\\\\\\\\\":\\\\\\\\\\\\\"3\\\\\\\\\\\\\",\\\\\\\\\\\\\"componentId\\\\\\\\\\\\\":\\\\\\\\\\\\\"61518\\\\\\\\\\\\\",\\\\\\\\\\\\\"level\\\\\\\\\\\\\":\\\\\\\\\\\\\"2\\\\\\\\\\\\\",\\\\\\\\\\\\\"itemPageNo\\\\\\\\\\\\\":\\\\\\\\\\\\\"0\\\\\\\\\\\\\",\\\\\\\\\\\\\"lastItemIndex\\\\\\\\\\\\\":\\\\\\\\\\\\\"0\\\\\\\\\\\\\",\\\\\\\\\\\\\"pageKey\\\\\\\\\\\\\":\\\\\\\\\\\\\"LOGICSHOW_LOGICTV_DEFAULT\\\\\\\\\\\\\",\\\\\\\\\\\\\"dataSourceType\\\\\\\\\\\\\":\\\\\\\\\\\\\"episode\\\\\\\\\\\\\",\\\\\\\\\\\\\"group\\\\\\\\\\\\\":\\\\\\\\\\\\\"0\\\\\\\\\\\\\",\\\\\\\\\\\\\"itemStartStage\\\\\\\\\\\\\":%start%,\\\\\\\\\\\\\"itemEndStage\\\\\\\\\\\\\":%end%}\\\\\",\\\\\"videoId\\\\\":\\\\\"%tvid%\\\\\",\\\\\"showId\\\\\":\\\\\"%showId%\\\\\"}\",\"system_info\":\"{\\\\\"os\\\\\":\\\\\"pc\\\\\",\\\\\"device\\\\\":\\\\\"pc\\\\\",\\\\\"ver\\\\\":\\\\\"1.0.0\\\\\",\\\\\"appPackageKey\\\\\":\\\\\"pcweb\\\\\",\\\\\"appPackageId\\\\\":\\\\\"pcweb\\\\\"}\"}";

    //private static String descData = "{\"ms_codes\":\"2019030100\",\"params\":\"{\\\\\"biz\\\\\":true,\\\\\"scene\\\\\":\\\\\"component\\\\\",\\\\\"componentVersion\\\\\":\\\\\"3\\\\\",\\\\\"ip\\\\\":\\\\\"113.128.101.51\\\\\",\\\\\"debug\\\\\":0,\\\\\"utdid\\\\\":\\\\\"1TH+GgpleHkCAXGAM+lnFijw\\\\\",\\\\\"userId\\\\\":\\\\\"\\\\\",\\\\\"platform\\\\\":\\\\\"pc\\\\\",\\\\\"nextSession\\\\\":\\\\\"{\\\\\\\\\\\\\"componentIndex\\\\\\\\\\\\\":\\\\\\\\\\\\\"1\\\\\\\\\\\\\",\\\\\\\\\\\\\"componentId\\\\\\\\\\\\\":\\\\\\\\\\\\\"46112\\\\\\\\\\\\\",\\\\\\\\\\\\\"level\\\\\\\\\\\\\":\\\\\\\\\\\\\"2\\\\\\\\\\\\\",\\\\\\\\\\\\\"itemPageNo\\\\\\\\\\\\\":\\\\\\\\\\\\\"0\\\\\\\\\\\\\",\\\\\\\\\\\\\"lastItemIndex\\\\\\\\\\\\\":\\\\\\\\\\\\\"100\\\\\\\\\\\\\",\\\\\\\\\\\\\"pageKey\\\\\\\\\\\\\":\\\\\\\\\\\\\"LOGICSHOW_LOGICTV_DEFAULT\\\\\\\\\\\\\",\\\\\\\\\\\\\"group\\\\\\\\\\\\\":\\\\\\\\\\\\\"46104\\\\\\\\\\\\\"}\\\\\",\\\\\"videoId\\\\\":\\\\\"%tvid%\\\\\",\\\\\"showId\\\\\":\\\\\"dfee54c74cc240e7ba04\\\\\"}\",\"system_info\":\"{\\\\\"os\\\\\":\\\\\"pc\\\\\",\\\\\"device\\\\\":\\\\\"pc\\\\\",\\\\\"ver\\\\\":\\\\\"1.0.0\\\\\",\\\\\"appPackageKey\\\\\":\\\\\"pcweb\\\\\",\\\\\"appPackageId\\\\\":\\\\\"pcweb\\\\\"}\"}";

    private static String PAGE_DATA = "{\"ms_codes\":\"2019030100\",\"params\":\"{\\\\\"biz\\\\\":\\\\\"new_detail_web\\\\\",\\\\\"videoId\\\\\":\\\\\"%tvid%\\\\\",\\\\\"scene\\\\\":\\\\\"web_page\\\\\",\\\\\"componentVersion\\\\\":\\\\\"3\\\\\",\\\\\"ip\\\\\":\\\\\"111.14.107.103\\\\\",\\\\\"debug\\\\\":0,\\\\\"utdid\\\\\":\\\\\"DL7eG1PvFwMCAXF4McZVeS9C\\\\\",\\\\\"userId\\\\\":0,\\\\\"platform\\\\\":\\\\\"pc\\\\\",\\\\\"nextSession\\\\\":\\\\\"{\\\\\\\\\\\\\"lastModuleId\\\\\\\\\\\\\":\\\\\\\\\\\\\"12024\\\\\\\\\\\\\",\\\\\\\\\\\\\"level\\\\\\\\\\\\\":\\\\\\\\\\\\\"0\\\\\\\\\\\\\",\\\\\\\\\\\\\"pageKey\\\\\\\\\\\\\":\\\\\\\\\\\\\"LOGICSHOW_CHILDREN_DEFAULT\\\\\\\\\\\\\",\\\\\\\\\\\\\"pageId\\\\\\\\\\\\\":\\\\\\\\\\\\\"2318\\\\\\\\\\\\\",\\\\\\\\\\\\\"lastModuleIndex\\\\\\\\\\\\\":\\\\\\\\\\\\\"1\\\\\\\\\\\\\"}\\\\\",\\\\\"gray\\\\\":0,\\\\\"showId\\\\\":\\\\\"%showId%\\\\\"}\",\"system_info\":\"{\\\\\"os\\\\\":\\\\\"pc\\\\\",\\\\\"device\\\\\":\\\\\"pc\\\\\",\\\\\"ver\\\\\":\\\\\"1.0.0\\\\\",\\\\\"appPackageKey\\\\\":\\\\\"pcweb\\\\\",\\\\\"appPackageId\\\\\":\\\\\"pcweb\\\\\"}\"}";
                                    //"{\"ms_codes\":\"2019030100\",\"params\":\"{\\\\\"biz\\\\\":true,\\\\\"scene\\\\\":\\\\\"page\\\\\",\\\\\"ip\\\\\":\\\\\"113.128.101.51\\\\\",\\\\\"debug\\\\\":0,\\\\\"utdid\\\\\":\\\\\"1TH+GgpleHkCAXGAM+lnFijw\\\\\",\\\\\"userId\\\\\":\\\\\"\\\\\",\\\\\"platform\\\\\":\\\\\"pc\\\\\",\\\\\"nextSession\\\\\":\\\\\"{\\\\\\\\\\\\\"lastModuleId\\\\\\\\\\\\\":\\\\\\\\\\\\\"11930\\\\\\\\\\\\\",\\\\\\\\\\\\\"level\\\\\\\\\\\\\":\\\\\\\\\\\\\"0\\\\\\\\\\\\\",\\\\\\\\\\\\\"pageKey\\\\\\\\\\\\\":\\\\\\\\\\\\\"LOGICSHOW_LOGICTV_DEFAULT\\\\\\\\\\\\\",\\\\\\\\\\\\\"pageId\\\\\\\\\\\\\":\\\\\\\\\\\\\"2302\\\\\\\\\\\\\",\\\\\\\\\\\\\"lastModuleIndex\\\\\\\\\\\\\":\\\\\\\\\\\\\"1\\\\\\\\\\\\\"}\\\\\",\\\\\"videoId\\\\\":\\\\\"%tvid%\\\\\",\\\\\"showId\\\\\":\\\\\"dfee54c74cc240e7ba04\\\\\"}\",\"system_info\":\"{\\\\\"os\\\\\":\\\\\"pc\\\\\",\\\\\"device\\\\\":\\\\\"pc\\\\\",\\\\\"ver\\\\\":\\\\\"1.0.0\\\\\",\\\\\"appPackageKey\\\\\":\\\\\"pcweb\\\\\",\\\\\"appPackageId\\\\\":\\\\\"pcweb\\\\\"}\"}";
    public List<FilmographyInfo> videoInfos(String start, String end, String tvid) throws ScriptException, IOException {
        String showId = getShowId(tvid);
        JSONObject obj = YoukuUtil.result(URLUtil.format(data, "0", "200", tvid, showId));
        obj = obj.getJSONObject("2019030100");
        obj = obj.getJSONObject("data");
        JSONArray jsonArray = obj.getJSONArray("nodes");
        return info(jsonArray);
    }
    public Filmography baseInfo(String docid, String tvid) {
        Filmography filmography = new Filmography();
        try {
            String showId = getShowId(tvid);
            JSONObject obj = YoukuUtil.result(URLUtil.format(PAGE_DATA, tvid, showId));
            filmography = filmography(obj);
            filmography.setUrl(format(tvid));
            filmography.setPlatform(PlatformSupport.PLATFORM_YOUKU);
            filmography.setPlatformName(PlatformSupport.PLATFORM_NAME_YOUKU);
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return filmography;
    }
    public String getShowId(String tvid) throws IOException {
        /*String url = "https://v.youku.com/v_show/id_" + tvid + ".html";
        String rsp = ClientUtil.httpGet(url);
        String[] d = StringUtil.split(rsp, "window.__INITIAL_DATA__ =");
        if (d.length < 2){
            return "";
        }
        d = StringUtil.split(d[1], ";</script>");
        if (d.length < 2) {
            return "";
        }
        try {
            JSONObject obj = JSONObject.parseObject(d[1]);
            return obj.getString("showId");
        }
        catch (JSONException e){
        }*/
        return "";
    }
    /*public Filmography baseInfo(String docid, String tvid) {
        Filmography filmography = new Filmography();
        try {
            JSONObject obj = YoukuUtil.result(URLUtil.format(descData, tvid));
            filmography = filmography(obj);
            filmography.setUrl(format(tvid));
            filmography.setPlatform(PlatformSupport.PLATFORM_YOUKU);
            filmography.setPlatformName(PlatformSupport.PLATFORM_NAME_YOUKU);
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return filmography;
    }*/
/*    public Filmography filmography(JSONObject jsonObj){
        Filmography filmography = new Filmography();
        jsonObj = jsonObj.getJSONObject("2019030100");
        jsonObj = jsonObj.getJSONObject("data");
        jsonObj.getJSONObject("data");

        JSONArray jsonArray = jsonObj.getJSONArray("nodes");
        JSONObject data = jsonArray.getJSONObject(0).getJSONObject("data");
        filmography.setDesc( data.getString("desc") );
        filmography.setTag(data.getString("showGenre"));
        filmography.setName("");
        return filmography;
    }*/

    public Filmography filmography(JSONObject jsonObj){
        Filmography filmography = new Filmography();
        jsonObj = jsonObj.getJSONObject("2019030100");
        jsonObj = jsonObj.getJSONObject("data");
        JSONObject temp = jsonObj.getJSONObject("data");
        JSONObject extra = temp.getJSONObject("extra");
        filmography.setTag(extra.getString("videoCategory"));
        filmography.setImg(extra.getString("showImgV"));
        filmography.setName(extra.getString("showName"));
        JSONArray jsonArray = jsonObj.getJSONArray("nodes");
        JSONArray array = jsonArray.getJSONObject(0).getJSONArray("nodes");
        JSONObject data = array.getJSONObject(0);
        array = data.getJSONArray("nodes");
        a(array, filmography);

        return filmography;
    }
    public void a(JSONArray array, Filmography filmography){
        // 基本信息组件
        JSONObject descComponent = new JSONObject();
        List<JSONObject> directorList = new ArrayList<>();
        List<JSONObject> actorList = new ArrayList<>();
        List<JSONObject> guestList = new ArrayList<>();
        for (int i = 0, l = array.size(); i < l; i++){
            JSONObject data = array.getJSONObject(i).getJSONObject("data");
            if ("导演".equals(data.getString("subtitle"))){
                directorList.add(data);
            }
            else if ("嘉宾".equals(data.getString("subtitle"))){
                guestList.add(data);
            }
            else if (StringUtil.isEmpty(data.getString("subtitle"))){
                descComponent = data;
            }
            else if (-1 != data.getString("subtitle").indexOf("饰")){
                actorList.add(data);
            }
        }
        filmography.setDesc( descComponent.getString("desc") );
        filmography.setVideoMate(descComponent.getString("updateInfo"));
        filmography.setYear(descComponent.getString("showReleaseYear"));
        ParticipationMate mate = new ParticipationMate();
        mate.setGuest(toParticipation(guestList));
        mate.setActor(toParticipation(actorList));
        mate.setDirector(toParticipation(directorList));
        filmography.setMate(mate);
    }
    public List<Participation> toParticipation(List<JSONObject> objectList){
        List<Participation> list = new ArrayList<>();
        for (JSONObject obj : objectList){
            Participation participation = new Participation();
            participation.setName(obj.getString("title"));
            participation.setPlatform(PlatformSupport.PLATFORM_YOUKU);
            participation.setId(obj.getString("personId"));
            participation.setImg(obj.getString("img"));
            list.add(participation);
        }
        return list;
    }
    public List<FilmographyInfo> info(JSONArray array){
        List<FilmographyInfo> infos = new ArrayList<>();
        int number = 0;
        for (int i = 0, l = array.size(); i < l; i ++){
            JSONObject obj = array.getJSONObject(i);
            obj = obj.getJSONObject("data");
            if (!"正片".equals(obj.getString("videoType"))){
                continue;
            }
            JSONObject action = obj.getJSONObject("action");
            FilmographyInfo info = new FilmographyInfo();
            info.setUrl(format(action.getString("value")));
            info.setNumber(++number);
            info.setPlatform(PlatformSupport.PLATFORM_YOUKU);
            info.setId(action.getString("value"));
            info.setTitle(obj.getString("title"));
            info.setDisplayMode(FilmographyInfo.DISPLAY_MODE_TITLE_BLOCK);
            infos.add(info);
        }
        return infos;
    }
    private String format(String tvid){
        return String.format("https://v.youku.com/v_show/id_%s.html", tvid);
    }
}
